<?php

class GetStudent
{
	private $sugar_client;
	private $wsdl;
	
	/* Constructor */
	public function __Construct($arg_wsdl)
	{
		//echo('GetStudent->Constructor('.$arg_wsdl.')');
		$this->wsdl = $arg_wsdl;
		$this->sugar_client = new nusoapclient($arg_wsdl, true);
	}
	
	/* Geef een array van alle studenten terug */
	public function getStudenten($arg_actief)
	{
		$active;
		if($arg_actief == 'Actief')
			$active = 'Actief';
		
		//echo('GetStudent->getStudenten()');
		$session_id = $_SESSION['session'];
		$set_entry_params = array(
								  'session' => $session_id,
								  'module_name' => 'st_Student',
								  'query' => '',
								  'order_by' => '',
								  'select_fields'=>array(
															'id',
															'student_nr',
															'name',
															'last_name',
															'betweentf',
															'education',
															'active'
														),
								  'deleted' => false
								  );
		
		//Parse de "ruwe" data naar objecten
		$parser = new Parser($this->wsdl);
		$parser->setResult($this->sugar_client->call('get_entry_list',$set_entry_params));
		
		$studentenArray = $parser->getStudentArray();
		
		$getStages = new GetStage($this->wsdl);
		$stagesArray = $getStages->getStages();
		
		foreach($studentenArray as $student)
		{
			$student_id = $student->getId();
			$stageArray = NULL;
			
			if(sizeof($stagesArray) > 0)
			{
				$stageArray = array();
				
				foreach($stagesArray as $stage)
				{
					$student1_id = $stage->getStudent1();

					if($student1_id == $student_id)
					{
						$stageArray[] = $stage;
					}
					
					if($stage->getStudent2() != NULL)
					{
						if($stage->getStudent2() == $student->getId())
						{
							$stageArray[] = $stage;
						}
					}
				}
			}
			
			$student->setStages($stageArray);
		}
		
		
		return $studentenArray;
	}
	
	public function getStudent($arg_student_id)
	{
		//echo('GetStudent->getStudent('.$arg_id.')');
		$session_id = $_SESSION['session'];
		$set_entry_params = array(
								  'session' => $session_id,
								  'module_name' => 'st_Student',
								  'id' => $arg_student_id,
								  'select_fields'=>array(
															'id',
															'name',
															'student_nr',
															'last_name',
															'betweentf',
															'education',
															'active'
														)
								  );
		
		//Parse de "ruwe" data naar objecten en geef deze vervolgens terug
		$parser = new Parser($this->wsdl);
		$result = $this->sugar_client->call('get_entry',$set_entry_params);
		
		$parser->setResult($result);
						   
		$student = $parser->getStudentArray();
		$student = $student[0];
		
		$getStages= new GetStage($this->wsdl);
		$stagesArray= $getStages->getStages();
		
		$student_id = $student->getId();
		$stageArray = NULL;
		
		if(sizeof($stagesArray) > 0)
		{
			$stageArray = array();
			
			foreach($stagesArray as $stage)
			{
				$student1_id = $stage->getStudent1();

				if($student1_id == $student_id)
				{
					$stageArray[] = $stage;
				}
				
				if($stage->getStudent2() != NULL)
				{
					if($stage->getStudent2() == $student->getId())
					{
						$stageArray[] = $stage;
					}
				}
			}
		}
		
		$student->setStages($stageArray);
		
		return $student;
	}
}

?>